package com.atguigu.juc.homework1;

/**
 *两个线程，一个线程打印1-52，另一个打印字母A-Z打印顺序为12A34B...5152Z,
 * 要求用线程间通信
 */

class Work{
    private int num1=1;
    private int num2=65;
    private int flag=0;

    public synchronized void printNumber(){
        //1.判断
        if (flag!=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //2.干活
        flag++;
        System.out.println(Thread.currentThread().getName()+"---"+(num1++));
        System.out.println(Thread.currentThread().getName()+"---"+(num1++));
        //3.通知
        this.notifyAll();
    }

    public  synchronized void printLatter(){
        //1.判断
        if (flag==0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //2.干活
        flag--;
        System.out.println(Thread.currentThread().getName()+"---"+(char)num2);
        num2++;
        //3.通知
        this.notifyAll();
    }

}
public class TheadHomeWork {
    public static void main(String[] args) {
        Work work = new Work();
        new Thread(() -> { for (int i = 0; i <26 ; i++) { work.printNumber();} },"A").start();
        new Thread(() -> { for (int i = 0; i <26 ; i++) { work.printLatter();} },"B").start();
    }
}
